package com.example.androidlearning.sy14;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.Poi;
import com.baidu.location.PoiRegion;
import com.baidu.mapapi.SDKInitializer;
import com.example.androidlearning.R;

public class LBSMainActivity extends AppCompatActivity {

    Button button;
    Button button1;
    TextView textView;

    LocationClient locationClient = null;
    LocationClientOption option = new LocationClientOption();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy1401_lbs);

        button = findViewById(R.id.sy14_getlocal);
        textView = findViewById(R.id.sy14_local);

        button1 = findViewById(R.id.sy14_getpoi);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationClient = new LocationClient(getApplicationContext());
                MyLocationListener myLocationListener = new MyLocationListener(0);
                locationClient.registerLocationListener(myLocationListener);
                option.setIsNeedAddress(true);
                locationClient.setLocOption(option);
                locationClient.start();
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationClient = new LocationClient(getApplicationContext());
                MyLocationListener myLocationListener = new MyLocationListener(1);
                locationClient.registerLocationListener(myLocationListener);
                option.setIsNeedAddress(true);
                option.setIsNeedLocationPoiList(true);
                locationClient.setLocOption(option);
                locationClient.start();
            }
        });

    }



    private class MyLocationListener implements BDLocationListener {

        public MyLocationListener(int type){
            this.type = type;
        }

        int type;
        @Override
        public void onReceiveLocation(BDLocation location) {
            StringBuffer sb = new StringBuffer(256);
            if(type == 0){
                sb.append("时间 : ").append(location.getTime())
                        .append("\n错误代码 : ").append(location.getLocType())
                        .append("\n纬度 : ").append(location.getLatitude())
                        .append("\n经度 : ").append(location.getLongitude())
                        .append("\n定位精度 : ").append(location.getRadius())
                        .append("\n省份 : ").append(location.getProvince())
                        .append("\n城市 : ").append(location.getCity())
                        .append("\n区/县 : ").append(location.getDistrict())
                        .append("\n街道 : ").append(location.getStreet())
                        .append("\n地址 : ").append(location.getAddrStr());
            }else {
                Poi poi = location.getPoiList().get(0);
                sb.append("POI名称 : ").append(poi.getName())
                .append("\nPOI类型 : ").append(poi.getTags())
                .append("\nPOI地址 : ").append(poi.getAddr());
                PoiRegion poiRegion = location.getPoiRegion();
                sb.append("\nPoiRegion位置关系 : ").append(poiRegion.getDerectionDesc())
                .append("\nPoiRegion名称 : ").append(poiRegion.getName())
                .append("\nPoiRegion类型 : ").append(poiRegion.getTags());
            }
            textView.setText(sb.toString());
        }
    }

}
